package com.gxuwz.stats.service.excelNewHandler;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.gxuwz.stats.entity.AcademicPapers;
import com.gxuwz.stats.entity.dto.AcademicPapersNewImportDTO;
import com.gxuwz.stats.service.excelhandler.ImportResultProvider;
import com.gxuwz.stats.service.impl.AcademicPapersServiceImpl;

import java.util.ArrayList;
import java.util.List;

public class AcademicPaperNewListener extends AnalysisEventListener<AcademicPapersNewImportDTO> implements ImportResultProvider {
    private final List<AcademicPapersNewImportDTO> dataList = new ArrayList<>();
    private final AcademicPapersServiceImpl workloadService;
    private final String currentUsername;
    private final String currentRole;
    private final String currentDepartment;
    private int ignoredCount = 0;

    public AcademicPaperNewListener(AcademicPapersServiceImpl workloadService, String username, String role, String department) {
        this.workloadService = workloadService;
        this.currentUsername = username;
        this.currentRole = role;
        this.currentDepartment = department;
    }

    @Override
    public void invoke(AcademicPapersNewImportDTO dto, AnalysisContext context) {
        if (dto.getUsername() == null || dto.getUsername().trim().isEmpty()) {
            return;
        }
        if (dto.getUsername().contains("说明") || dto.getUsername().length() > 12) {
            return;
        }
        if ("普通用户".equals(currentRole)) {
            if (!dto.getUsername().equals(currentUsername)) {
                ignoredCount++;
                return;
            }
        } else if ("二级学院管理员".equals(currentRole)) {
            // 这里可以加部门校验
        }
        dataList.add(dto);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        workloadService.saveAllFromNewDTO(dataList);
    }

    @Override
    public int getImportedCount() {
        return dataList.size();
    }

    @Override
    public int getIgnoredCount() {
        return ignoredCount;
    }
}
